package com.baizhi.zjs.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.dao.OrderDAO;
import com.baizhi.zjs.entity.FUSerEntity;
import com.baizhi.zjs.entity.FrontAddressEntity;
import com.baizhi.zjs.entity.OrderEntity;
import com.baizhi.zjs.util.MybatisUtil;
import com.baizhi.zjs.util.SnowflakeIdWorker;
import com.baizhi.zjs.util.UUIDUtil;

public class OrederServiceImpl implements OrderServict{
	/*id                   VARCHAR2(40)                    not null,
	   order_number         VARCHAR2(30),    --订单编号
	   order_times          DATE,            --订单时间
	   addr_user            VARCHAR2(30),    --收货人
	   addr_name            VARCHAR2(30),    --收货地址
	   total_price          NUMERIC(12, 2),  --总价
	   addr_id              VARCHAR2(40),    --地址id
	   user_id              VARCHAR2(40),    --用户id*/
	@Override
	public OrderEntity addOrder(FrontAddressEntity fra) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrderDAO dao = sqlSession.getMapper(OrderDAO.class);
		//OrderEntity entity=null;
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//获取用户信息
	FUSerEntity fuser = (FUSerEntity)session.getAttribute("login");
	//获取购物车中的总价
	double totalPrice = (double)session.getAttribute("totalPrice");
	//uuid生成id
	String uuid = UUIDUtil.getUuid();
	//SnowflakeIdWorker  雪花算法，生成带有时间戳的订单编号
	String snowflakeId = SnowflakeIdWorker.getOrderNumber();
	//创建订单对象
	OrderEntity	entity = new OrderEntity(uuid,snowflakeId, new Date(),fra.getName(),fra.getDetail(),totalPrice, fra.getId(),fuser.getId());
	//添加到数据库
	try {
		dao.addOrder(entity);
		MybatisUtil.commit(sqlSession);
		MybatisUtil.close(sqlSession);
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		MybatisUtil.rollback(sqlSession);
		MybatisUtil.close(sqlSession);
	}
	
	return entity;
	}
// 查询所有订单          action在地址Action中
	@Override
	public List<OrderEntity> showAllOrder() {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrderDAO dao = sqlSession.getMapper(OrderDAO.class);
		List<OrderEntity> list = dao.showAllOrder();
		MybatisUtil.close(sqlSession);
		return list;
	}
	@Override
	public OrderEntity  showoneOrder(String ss) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		OrderDAO dao = sqlSession.getMapper(OrderDAO.class);
		OrderEntity list = dao.showoneOrder(ss);
		return list;
	}

}
